package com.jdew.thkj.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;

import java.util.HashMap;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangyang on 2017/9/7.
 */

public class ModifyPassWordActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.oldmima)
    EditText oldmima;
    @Bind(R.id.newmima)
    EditText newmima;
    @Bind(R.id.mimasure)
    EditText mimasure;
    @Bind(R.id.modify)
    Button modify;
    SharedPreferences sp;
    SharedPreferences.Editor editor;
    @Override
    public int bindLayout() {
        return R.layout.activity_modify_pw;
    }

    @Override
    public void initView(View view) {
        sp= getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        back.setVisibility(View.VISIBLE);
        title.setVisibility(View.VISIBLE);
        back.setOnClickListener(this);
        modify.setOnClickListener(this);
        title.setText("修改密码");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.modify: {
              if(TextUtils.isEmpty(oldmima.getText())){
                  ToastUtils.getInstance(this).showMessage("请输入原始密码");
              }else if(TextUtils.isEmpty(newmima.getText())){
                  ToastUtils.getInstance(this).showMessage("请输入新密码");
              }else if(newmima.getText().length()<6||newmima.getText().length()>16){
                  ToastUtils.getInstance(this).showMessage("密码长度需要在6-16位之间");
              }else if(TextUtils.isEmpty(mimasure.getText())){
                  ToastUtils.getInstance(this).showMessage("请再次确认新密码");
              }else if(!newmima.getText().toString().trim().equals(mimasure.getText().toString().trim())){
                  ToastUtils.getInstance(this).showMessage("两次新密码不一致,请重新确认");
              }else if(newmima.getText().toString().trim().equals(oldmima.getText().toString().trim())){
                  ToastUtils.getInstance(this).showMessage("新密码与原密码不能相同");
              }else{
                  ModifyUserPsw();
              }
            }
            break;
            default:
                break;
        }

    }
     public void ModifyUserPsw(){
         String oldPsw=oldmima.getText().toString().trim();
         String timesamp= RegexValidateUtils.getTime13();
         String numsamp=RegexValidateUtils.getRandom10();
         HashMap<String,Object> map=new HashMap();
         map.put("UserPwd",oldPsw);
         map.put("UserNewPwd",newmima.getText().toString().trim());
         map.put("UserPhone",sp.getString("userPhone",""));
         RetrofitManager.getInstance( AppConfig.BASE_URL)
                 .createReq(RequestApi.class).
                 SetLoginPwd(map,RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken","")+"UserPhone"+sp.getString("userPhone","")+"UserPwd"+oldPsw+"UserNewPwd"+newmima.getText().toString().trim())).toUpperCase())
                 .observeOn( AndroidSchedulers.mainThread()).subscribeOn( Schedulers.io()).subscribe( new Subscriber<JSONObject>() {
             @Override
             public void onCompleted() {
             }

             @Override
             public void onError(Throwable e) {
             }

             @Override
             public void onNext(JSONObject jsonObject) {
                 if(jsonObject.get("code").equals("0")){
                     editor.putString( "psw", mimasure.getText().toString().trim());
                     editor.apply();
                     ToastUtils.getInstance(ModifyPassWordActivity.this).showMessage("密码修改成功，请重新登陆");
                     startActivity( LoginActivity.class);
                 }else if(jsonObject.get("code").equals("-2")){
                     ToastUtils.getInstance(ModifyPassWordActivity.this).showMessage("此手机号没有注册");
                 }else if(jsonObject.get("code").equals("-3")){
                     ToastUtils.getInstance(ModifyPassWordActivity.this).showMessage("原密码输入错误");
                 }
             }
         });
     }

}
